<?php
    function totalJokes($pdo){
        $sql = 'SELECT COUNT(*) FROM joke';
        $query = query($pdo,$sql);
        $row = $query->fetch();
        return $row[0];
    }

    //查询函数
    function query($pdo,$sql,$parameters=[]){
        $query = $pdo->prepare($sql);
        $query->execute($parameters);
        return $query;
    }

    //获得笑话
    function getJoke($pdo,$id){
        $sql = 'SELECT * FROM `joke` WHERE  `id`=:id';
        $parameters = [':id'=>$id];
        $query = query($pdo,$sql,$parameters);
        return $query->fetch();
    }
    //插入笑话语句
    function insertJoke($pdo,$joketext,$authorId){
        $query = 'INSERT INTO joke SET
        joketext = :joketext,
        jokedate = CURDATE(),
        authorId = :authorId';
        $parameters = [':joketext'=>$joketext,':authorId'=>$authorId];
        query($pdo,$query,$parameters);
    }